package xy.come.juhenews.Adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhy.autolayout.utils.AutoUtils;

import java.text.DecimalFormat;
import java.util.List;

import xy.come.juhenews.Activity.WeatherActivity;
import xy.come.juhenews.Bean.WeatherDayBean;
import xy.come.juhenews.Http.Constant;
import xy.come.juhenews.R;

/**
 * Created by Xia_焱 on 2018/8/27.
 * e-mail：XiahaotianV@163.com
 */

public class WeatherDayAdapter extends RecyclerView.Adapter<WeatherDayAdapter.ViewHoldX> {

    private Context mContext;
    private List<WeatherDayBean.HeWeather6Bean.DailyForecastBean> mList;

    public WeatherDayAdapter(Context ctx, List<WeatherDayBean.HeWeather6Bean.DailyForecastBean> list) {
        this.mContext = ctx;
        this.mList = list;
    }

    @NonNull
    @Override
    public WeatherDayAdapter.ViewHoldX onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_weather_day, parent, false);
        ViewHoldX viewHoldX = new ViewHoldX(inflate);
        return viewHoldX;
    }

    @Override
    public void onBindViewHolder(@NonNull WeatherDayAdapter.ViewHoldX holder, int position) {
        String Xcode = mList.get(position).getCond_code_d();
        if (Xcode.equals("100")) {
            Drawable drawable = mContext.getResources().getDrawable(R.mipmap.t_sun);
            holder.iv_tu.setBackgroundDrawable(drawable);
        } else if (Xcode.equals("101") || Xcode.equals("102") || Xcode.equals("103")) {
            Drawable drawable = mContext.getResources().getDrawable(R.mipmap.t_dy);
            holder.iv_tu.setBackgroundDrawable(drawable);
        } else if (Xcode.equals("200") || Xcode.equals("201") || Xcode.equals("202") || Xcode.equals("203") || Xcode.equals("204") || Xcode.equals("205") || Xcode.equals("206") || Xcode.equals("207") || Xcode.equals("208") || Xcode.equals("209") || Xcode.equals("210")) {
            Drawable drawable = mContext.getResources().getDrawable(R.mipmap.t_wind);
            holder.iv_tu.setBackgroundDrawable(drawable);
        } else if (Xcode.equals("104")) {
            Drawable drawable = mContext.getResources().getDrawable(R.mipmap.t_yin);
            holder.iv_tu.setBackgroundDrawable(drawable);
        } else if (Xcode.equals("300") || Xcode.equals("301") || Xcode.equals("305") || Xcode.equals("306") || Xcode.equals("399") || Xcode.equals("314") || Xcode.equals("309")) {
            Drawable drawable = mContext.getResources().getDrawable(R.mipmap.t_rain);
            holder.iv_tu.setBackgroundDrawable(drawable);
        } else if (Xcode.equals("302") || Xcode.equals("303") || Xcode.equals("304") || Xcode.equals("318") || Xcode.equals("317") || Xcode.equals("316") || Xcode.equals("315") || Xcode.equals("312") || Xcode.equals("311") || Xcode.equals("310") || Xcode.equals("318") || Xcode.equals("308") || Xcode.equals("307")) {
            Drawable drawable = mContext.getResources().getDrawable(R.mipmap.t_d_rain);
            holder.iv_tu.setBackgroundDrawable(drawable);
        } else if (Xcode.equals("400") || Xcode.equals("401") || Xcode.equals("402") || Xcode.equals("408") || Xcode.equals("409") || Xcode.equals("410") || Xcode.equals("407") || Xcode.equals("499")) {
            Drawable drawable = mContext.getResources().getDrawable(R.mipmap.t_sonw);
            holder.iv_tu.setBackgroundDrawable(drawable);
        } else if (Xcode.equals("503") || Xcode.equals("504") || Xcode.equals("507") || Xcode.equals("508")) {
            Drawable drawable = mContext.getResources().getDrawable(R.mipmap.t_sand);
            holder.iv_tu.setBackgroundDrawable(drawable);
        } else if (Xcode.equals("509") || Xcode.equals("510") || Xcode.equals("511") || Xcode.equals("512") || Xcode.equals("513") || Xcode.equals("514") || Xcode.equals("515")) {
            Drawable drawable = mContext.getResources().getDrawable(R.mipmap.t_haze);
            holder.iv_tu.setBackgroundDrawable(drawable);
        }
        String cond_txt_d = mList.get(position).getCond_txt_d();
        holder.tv_weather.setText(cond_txt_d);
        DecimalFormat df = new DecimalFormat("#########.#");
        String tmp_min= df.format((Float.parseFloat(mList.get(position).getTmp_min()) - 32) / 1.8);
        DecimalFormat dfX = new DecimalFormat("#########.#");
        String tmp_max= dfX.format((Float.parseFloat(mList.get(position).getTmp_max()) - 32) / 1.8);
        holder.tv_x_temperature.setText(tmp_min+"℃ / "+tmp_max+"℃");
        if (position == 0) {
            holder.tv_day.setText("今天");
        } else if (position == 1) {
            holder.tv_day.setText("明天");
        } else if (position == 2) {
            holder.tv_day.setText("后天");
        }


    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : 3;
    }

    class ViewHoldX extends RecyclerView.ViewHolder {

        ImageView iv_tu;
        TextView tv_day;
        TextView tv_weather;
        TextView tv_x_temperature;

        public ViewHoldX(View itemView) {
            super(itemView);
            AutoUtils.autoSize(itemView);
            iv_tu = itemView.findViewById(R.id.iv_tu);
            tv_day = itemView.findViewById(R.id.tv_day);
            tv_weather = itemView.findViewById(R.id.tv_weather);
            tv_x_temperature = itemView.findViewById(R.id.tv_x_temperature);
        }
    }
}
